public class PrimeNumbers {
    public static void main(String[] args) {
        int max = 20000;
        boolean[] isPrime = sieveOfEratosthenes(max);
        int count = 0;
        for (int i = 2; i <= max; i++) {
            if (isPrime[i]) {
                System.out.printf("%-6d", i);
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
    }
    
    private static boolean[] sieveOfEratosthenes(int max) {
        boolean[] isPrime = new boolean[max + 1];
        for (int i = 2; i <= max; i++) {
            isPrime[i] = true;
        }
        for (int p = 2; p * p <= max; p++) {
            if (isPrime[p]) {
                for (int i = p * p; i <= max; i += p) {
                    isPrime[i] = false;
                }
            }
        }
        return isPrime;
    }
}
    